﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotaHelp.Helpers;

namespace System.Web.Mvc
{
    public static class UserHelper
    {
        public static string GetUserName(this HttpRequestBase request)
        {
            return request.IsAuthenticated ? request.RequestContext.HttpContext.User.Identity.Name
                : (request.Cookies["userName"] != null ? request.Cookies["userName"].Value : "");
        }

        public static string PrepareUserName(this HttpContextBase context, string userName)
        {
            userName = ClearUserName(userName);

            if (context.Request.IsAuthenticated)
            {
                return (context.User.IsInRole("Admin") ? "_admin_" : "_vip_") + context.User.Identity.Name;
            }
            else if (userName.StartsWith("_admin_") || userName.StartsWith("_vip_"))
            {
                userName = "!" + userName;
            }

            return userName;
        }

        public static MvcHtmlString ShowUserName(this HtmlHelper html, string userName)
        {
            var urlhelper = new UrlHelper(html.ViewContext.RequestContext);

            string newUserName = ClearUserName(userName);
            Tag special = null; 

            if (userName.StartsWith("_admin_"))
            {
                special = new Tag("img", new { src = urlhelper.Content("~/Content/admin.gif") });
                newUserName = userName.Remove(0, "_admin_".Length);
            }
            else if (userName.StartsWith("_vip_"))
            {
                special = new Tag("img", new { src = urlhelper.Content("~/Content/vip.png") });
                newUserName = userName.Remove(0, "_vip_".Length);
            }

            newUserName = ClearUserName(newUserName);

            return MvcHtmlString.Create("<nobr><b>" + html.Encode(newUserName) + "</b>" + (special != null ? special.ToString() : "") + "</nobr>");            
        }

        private static string ClearUserName(string newUserName)
        {
            if (newUserName.StartsWith("!_vip_")) newUserName = newUserName.Remove(0, "!_vip_".Length);
            if (newUserName.StartsWith("!_admin_")) newUserName = newUserName.Remove(0, "!_admin_".Length);
            return newUserName;
        }

        public static void SaveUserName(this HttpResponseBase response, string userName)
        {
            var cookie = response.Cookies["userName"];
            cookie.Value = ClearUserName(userName);
            cookie.Expires = DateTime.Now.AddYears(5);  
        }

        public static string GetUserId(this HttpContextBase context)
        {
            var id = context.Request.Cookies["sdfsE12o"] != null ? context.Request.Cookies["sdfsE12o"].Value : "";

            if (string.IsNullOrWhiteSpace(id))
            {
                id = context.Response.Cookies["sdfsE12o"] != null ? context.Response.Cookies["sdfsE12o"].Value : "";
            }

            if (string.IsNullOrWhiteSpace(id))
            {
                var rnd = System.Security.Cryptography.RNGCryptoServiceProvider.Create();
                var bts = new byte[10];
                rnd.GetBytes(bts);
                id = string.Join("-", bts.Select(_=>_.ToString("000")));

                var cookie = context.Response.Cookies["sdfsE12o"];
                cookie.Value = id;
                cookie.Expires = DateTime.Now.AddYears(5); 
               
            }

            return id;
        }               
    }


}